//
//  HRZShoppingCartGoodsCell.m
//  EMarket
//
//  Created by 石头 on 2018/10/16.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZShoppingCartGoodsCell.h"
// Controllers

// Models
#import "HRZCartGoodsDetailModel.h"
// Views
#import "HRZNumberOfChoiesView.h"
// Vendors
#import <UIImageView+WebCache.h>
//#import <Messages/Messages.h>
//#import <YYKit.h>
// Categories

// Others

@interface HRZShoppingCartGoodsCell ()<HRZNumberOfChoiesViewDelegate>{
    HRZCartGoodsDetailModel *_model;
}

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIImageView *goodsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) HRZNumberOfChoiesView *numChangeView;

@end

@implementation HRZShoppingCartGoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addSubview:self.selectBtn];
        [self addSubview:self.goodsImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.descLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.numChangeView];
    }
    return self;
}

- (void)layoutSubviews {
//    [super layoutSubviews];
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self.height);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(30);
    }];
    
    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).mas_offset(10);
        make.bottom.mas_equalTo(self).mas_offset(-10);
        make.left.mas_equalTo(self.selectBtn.mas_right).mas_offset(5);
        make.width.mas_equalTo(self.height - 20);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_top);
        make.left.mas_equalTo(self.height + 30);
        make.right.mas_equalTo(self).mas_offset(20);
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(4);
        make.left.mas_equalTo(self.height + 30);
        make.right.mas_equalTo(self).mas_offset(20);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.height + 30);
        make.bottom.mas_equalTo(self).mas_offset(-10);
    }];
    
    [self.numChangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).mas_offset(-20);
        make.bottom.mas_equalTo(self).mas_offset(-10);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    
}

#pragma mark - Delegate
- (void)onCountChanged:(NSNumber *)number {
    [self onNumChangedWithCount:number];
}

#pragma mark - EventResponse
- (void)onSelectedBtnClick {
    _model.isSelect = !_model.isSelect;
    
    [self setSelectedBtnImage];
    
    [self sendDelegate];
}

- (void)onNumChangedWithCount:(NSNumber *)count {
    _model.goodsNumber = [count stringValue];
    
    [self sendDelegate];
}

- (void)setSelectedBtnImage {
    if (_model.isSelect) {
        [self.selectBtn setImage:IMAGE_NAMED(@"clicked") forState:UIControlStateNormal];
    } else {
        [self.selectBtn setImage:IMAGE_NAMED(@"unClick") forState:UIControlStateNormal];
    }
}

- (void)sendDelegate {
    if (self.delegate && [self.delegate respondsToSelector:@selector(onShoppingCartGoodsCell:changeModel:)]) {
        [self.delegate onShoppingCartGoodsCell:self changeModel:_model];
    }
}


#pragma mark - Getter/Setter
- (void)setModel:(HRZCartGoodsDetailModel *)model {
    _model = model;
    
    [self.goodsImageView sd_setImageWithURL:URLString(model.goodsImg) placeholderImage:PLACEHOLDERIMAGE];
    self.titleLabel.text = model.goodsName;
    self.descLabel.text = @"空空如也";
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@", model.price];
    self.numChangeView.countNum = [NSNumber numberWithInt:[model.goodsNumber intValue]];
    
    [self setSelectedBtnImage];
}

-(HRZCartGoodsDetailModel *)model {
    return _model;
}

- (UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_selectBtn addTarget:self action:@selector(onSelectedBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_selectBtn setImage:IMAGE_NAMED(@"unClick") forState:UIControlStateNormal];
    }
    return _selectBtn;
}

- (UIImageView *)goodsImageView{
    if (!_goodsImageView) {
        _goodsImageView = [[UIImageView alloc] init];
        [_goodsImageView setContentMode:UIViewContentModeScaleAspectFit];
    }
    return _goodsImageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = PFR15Font;
    }
    return _titleLabel;
}

- (UILabel *)descLabel{
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.font = PFR13Font;
        _descLabel.textColor = [UIColor grayColor];
    }
    return _descLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = PFR14Font;
        _priceLabel.textColor = [UIColor redColor];
    }
    return _priceLabel;
}

- (HRZNumberOfChoiesView *)numChangeView{
    if (!_numChangeView) {
        _numChangeView = [[HRZNumberOfChoiesView alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
        _numChangeView.delegate = self;
    }
    return _numChangeView;
}

@end
